package com.company.bookstore.validator.user_admin_module;

import com.company.bookstore.entities.user_admin_module.Roles;
import com.company.bookstore.exception.user_admin_module.CustomException;
import com.company.bookstore.qo.user_admin_module.AdminQo;
import com.company.bookstore.service.user_admin_module.AdminService;
import com.company.bookstore.service.user_admin_module.RoleService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.regex.Matcher;

@Component

public class AdminQoUpdateValidator extends AbstractValidatorHandler implements InitializingBean {
    @Resource
    AdminService adminService;
    @Resource
    RoleService roleService;
    @Override
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory.register(this.getClass().getName(),this);
    }
    public boolean doValidator(Object object) throws CustomException {
        AdminQo adminQo = (AdminQo)object;
        if (adminQo.getId() == null){
            throw   new CustomException("未指定管理员用户",403);
        }
        boolean hasPassword =  adminQo.getPassword()!=null;
        boolean hasUsername =  adminQo.getAdminName()!=null;
        Matcher correctUsername;
        boolean hasCorrectUsername;
        Matcher correctPassword;
        boolean hasCorrectPassword;
        if (hasUsername){
            correctUsername = AdminQoRegisterValidator.usernamePattern.matcher(adminQo.getAdminName());
            hasCorrectUsername = correctUsername.matches();
            if (!hasCorrectUsername){
                throw  new CustomException("账号账户名格式不正确，要求8-20位字母，数字，不能含有标点符号",403);
            }
            if (adminService.loadUserByUsername(adminQo.getAdminName())!=null){
                throw   new CustomException("该账户已存在",403);
            }
        }
        if (hasPassword){
            correctPassword = AdminQoRegisterValidator.passwordPattern.matcher(adminQo.getPassword());
            hasCorrectPassword = correctPassword.matches();
            if (!hasCorrectPassword){
                throw  new CustomException("账号账户名格式不正确，要求8-20位字母，数字，可含有标点符号",403);
            }
        }
        if ( adminQo.getDeleted()!=null ){
            if (adminQo.getDeleted() !=0 && adminQo.getDeleted()!=1){
                throw  new CustomException("参数格式不正确，设置删除状态失败",403);
            }
        }
        if (adminQo.getEnabled()!=null){
            if (adminQo.getEnabled() !=0 && adminQo.getEnabled()!=1){
                throw  new CustomException("参数格式不正确，设置可用状态失败",403);
            }
        }
        if (adminQo.getFkRoleId() !=null){
            Roles roles = roleService.getById(adminQo.getFkRoleId());
            if (roles==null){
                throw  new CustomException("没有该角色信息",403);
            }
            if (roles.getRoleName().equals("ROLE_ADMIN")){
                throw  new CustomException("无法修改为管理员角色",403);
            }
        }
        return true;
    }

}
